package per.chenxin.scale;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.List;

import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class HtmlUtil {

    final static String STR_CHARSET = "charset=";

    final static String STR_PRE_HTML = "<html";

    final static String STR_SUB_HTML = "</html>";

    static public String getCharset(byte[] bytesHttpResponse) {

        String strHttpResponse = new String(bytesHttpResponse);
        int beginIndex = strHttpResponse.indexOf(STR_CHARSET);
        if (beginIndex > 0) {
            return strHttpResponse.substring(beginIndex + STR_CHARSET.length(),
                strHttpResponse.indexOf("\n", beginIndex) - 1).replaceAll("\"", "");
        }
        return null;
    }

    static public String getHtmlFromBytes(byte[] bytesHttpResponse)
        throws UnsupportedEncodingException {

        return getHtmlFromBytes(bytesHttpResponse);
    }

    static public String getHtmlFromBytes(byte[] bytesHttpResponse, String strCharset)
        throws UnsupportedEncodingException {

        if (strCharset == null) {
            strCharset = getCharset(bytesHttpResponse);
        }

        String strHttpResponse = strCharset == null ? new String(bytesHttpResponse) : new String(
            bytesHttpResponse, strCharset);
        int beginIndex = strHttpResponse.indexOf(STR_PRE_HTML);
        int endIndex = strHttpResponse.indexOf(STR_SUB_HTML) + STR_SUB_HTML.length();
        return strHttpResponse.substring(beginIndex, endIndex);
    }

    static public List<String> getLinks(String strHtml, String strCharset) throws ParserException,
        IOException {

        List<String> listURL = new LinkedList<String>();
        Parser parser = Parser.createParser(strHtml, strCharset);
        NodeFilter filter;
        filter = new NodeClassFilter(LinkTag.class);
        NodeList list = parser.extractAllNodesThatMatch(filter);

        //OutputStreamWriter output = new OutputStreamWriter(
        //      new FileOutputStream("1111.txt"), "UTF-8");
        for (int i = 0; i < list.size(); i++) {
            listURL.add(((LinkTag) list.elementAt(i)).getLink());

            //            String strHtml = list.elementAt (i).toHtml ();
            //            if(strHtml.indexOf("messages")>0){
            //                //System.out.println (((LinkTag)list.elementAt (i)).getLink());
            //            //  output.write(list.elementAt (i).toHtml ());
            //            }
        }

        return listURL;
    }

}
